<!--
 * @Author: 周江英 10873108+zhou-jiangying@user.noreply.gitee.com
 * @Date: 2023-01-18 20:30:16
 * @LastEditors: 周江英 10873108+zhou-jiangying@user.noreply.gitee.com
 * @LastEditTime: 2023-01-20 11:04:52
 * @FilePath: \vue-shop\src\components\Home.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>

  <el-container class="home-container">
  <el-header>
    <div>
      <img src="../assets/heima.png" alt="">
      <span>仿电商后台管理系统</span>
    </div>
    <el-button type="info" @click="logout">退出</el-button>
  </el-header>
  <el-container>
    <el-aside :width="isCollapse ? '64px' : '200px' ">
      <!--折叠菜单栏-->
      <div class="toggle-button" @click="toggleCollapse">|||</div>
      <el-menu
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409EFF"
      :collapse-transition="false"
      :collapse="isCollapse" router
      :default-active="activePath">
      <!--collapse-transition：取消折叠动画、collapse：是否折叠菜单、router:为整个侧边栏开启路由跳转模式 default-active：当前激活菜单的 index-->

      <!--一级标题-->
      <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
        <template slot="title">
          <!--图标-->
          <i :class=iconsObj[item.id]></i>
          <!--标题-->
          <span>{{item.authName}}</span>
        </template>

        <!--二级标题-->
          <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
           @click="saveNavStatus('/'+subItem.path)">
            <template slot="title">
               <!--图标-->
              <i class="el-icon-menu"></i>
               <!--标题-->
              <span>{{subItem.authName}}</span>
            </template>
          </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-yonghu1',
        103: 'iconfont icon-mima5',
        101: 'iconfont icon-chanpinguanli',
        102: 'iconfont icon-tianjia',
        145: 'iconfont icon-tongji3'
      },
      // 是否折叠菜单 Boolean值
      isCollapse: false,
      // 被激活链接的地址
      activePath: ''
    }
  },
  created() {
    this.getMeumList()
    // 被创建的时候，就从本地存储中取值赋值给this.activePath
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMeumList() {
      const { data: res } = await this.$http.get('Menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavStatus(activePath) {
      // 把activePath='/'+subItem.path 存储到本地中
      window.sessionStorage.setItem('activePath', activePath)
      // 把本地存储的activePath值赋值给this.activePath
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #373d41;
  display: flex; // 弹性盒对象
  justify-content: space-between;// 均匀排列每个元素 首个元素放置于起点，末尾元素放置于终点
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div{
    display: flex;
    align-items: center; // 居中对齐弹性盒的
    span{
      margin-left: 15px;
    }
  }
}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
el-main{
  background-color: #eaedf1;
}
.iconfont{
  margin-right: 15px;
}
.toggle-button{
  background-color: #a9aaad;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
